package com.time.company.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Bing on 2018/1/3.
 * E-mail is tx16bing@163.com
 */

@SuppressLint("AppCompatCustomView")
public class FlickerTaskTV extends TextView {

    private boolean flag = false;

    public FlickerTaskTV(Context context) {
        this(context, null);
    }

    public FlickerTaskTV(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public FlickerTaskTV(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        flag = false;
        init();
    }

    private void init() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {

            public void run() {
                flag = !flag;
                postInvalidate();
            }
        };

        // 参数分别是delay（多长时间后执行），duration（执行间隔）
        timer.schedule(task, 1000, 1000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (flag) {
            setTextColor(0x00000000);
        } else {
            setTextColor(0xFFffffff);
        }

    }

}
